package com.vip.jwt.service.impl;

import cn.hutool.core.date.DateUtil;
import com.vip.jwt.common.utils.ClientUtil;
import com.vip.jwt.entity.LoggerOnline;
import com.vip.jwt.mapper.LoggerOnlineMapper;
import com.vip.jwt.service.LoggerOnlineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 登录日志 服务实现类
 * </p>
 *
 * @author vip
 * @since 2021-05-28
 */
@Service
public class LoggerOnlineServiceImpl extends ServiceImpl<LoggerOnlineMapper, LoggerOnline> implements LoggerOnlineService {

    @Resource
    HttpServletRequest httpServletRequest;

    @Resource
    private LoggerOnlineMapper loggerOnlineMapper;

    @Override
    public int saveLoginLogger(LoggerOnline loggerOnline) {
        loggerOnline.setType("登录");
        loggerOnline.setIp(ClientUtil.getClientIpAddress(httpServletRequest));
        loggerOnline.setDevice(ClientUtil.getDevice(httpServletRequest));
        loggerOnline.setLogTime(DateUtil.date());
        loggerOnlineMapper.insert(loggerOnline);
        return 0;
    }

    @Override
    public int saveLogoutLogger(LoggerOnline loggerOnline) {
        loggerOnline.setType("登出");
        loggerOnline.setIp(ClientUtil.getClientIpAddress(httpServletRequest));
        loggerOnline.setDevice(ClientUtil.getDevice(httpServletRequest));
        loggerOnline.setLogTime(DateUtil.date());
        loggerOnlineMapper.insert(loggerOnline);
        return 0;
    }

}
